<?php

class CpproductsController extends Core_Controller_Action 
{
	private $_products;
	
	public function init() {
		$this->_products = new Products();
	}
	
	public function indexAction() {
		$this->_forward( 'list', 'cpproducts' );
	}
	
	public function itemAction() {
		$id = intval( $this->_request->getParam( 'id', 0 ) );
		
		$ajax = intval( $this->_request->getParam( 'ajax', 0 ) );
		
		$galleryid = intval( $this->_request->getParam( 'galleryid', 0 ) );
		
		if( $ajax ) {
			$Photos = new Photos();

			$list = $Photos->getList( $galleryid );

			echo Zend_Json::encode( $list->toArray() );
			exit;
		}
 		
		$this->view->fileTypes = '"*.jpg"';
		$this->view->fileTypesDescription = 'Tylko zdjęcia';
		$this->view->uploadSuccess = 'uploadSuccessGalleryPopover';
		
		$this->view->listGallery = array();

		$Galeries = new Galeries();
		$this->view->galleriesList = $Galeries->getList();
		
		$Vat = new Vat();
		$this->view->vatList = $Vat->getList();
		
		$Categories = new Categries();
		$this->view->categoriesList = $Categories->getList( 0 );
		
		$Tags = new Tags();
		$this->view->tagList = $Tags->getList();
		
		$Manufacturers = new Manufacturers();
		$this->view->manufacturersList = $Manufacturers->getList();
		
		$this->view->categories = array(0);
		$this->view->tags = array(0);

		if( $this->_request->isPost() ) {

			$post = $this->_request->getPost();
			if( empty( $post['name_pl'] ) ) {
				$this->view->err_name_pl = 'Podaj nazwe';

				foreach( $post as $key => $value ) {
					$this->view->{$key} = $value;
				}

				if( $id ) {
					$this->view->p_id = $id;
					$CategriesHasProducts = new CategriesHasProducts();
					foreach( $CategriesHasProducts->getByProduct( $id )->toArray() as $value ) {
						$this->view->categories[] = $value['categries_c_id'];
					}
					
					$TagsHasProducts = new TagsHasProducts();
					foreach( $TagsHasProducts->getByProduct( $id )->toArray() as $value ) {
						$this->view->tags[] = $value['tags_t_id'];
					}

				}

			} else {

				if( $id ) {
					$this->view->p_id = $id;

					$conn = Doctrine_Manager::getInstance()->getCurrentConnection();

					try {
						$conn->beginTransaction();
					
						$item = $this->_products->getItem( $id )->getFirst();
						foreach( $post as $key => $value ) {
		
							if( 
								( $key != 'categories' ) && 
								( $key != 'tags' ) && 
								( strlen( $value ) > 0 ) 
							) {
								$item->{$key} = $value;
							}
	
							$this->view->{$key} = $value;
						}
						
						$item->mod_date = Date( 'Y-m-d H:i:s' );
						
						$item->save();
						
						if( $post['wishlist'] == 'no' ) {
							
							$Wishlist = new Wishlist();
							$listw = $Wishlist->getListByProduct( $id );

							if( count( $listw ) ) {
								$Users = new Users();
								
								foreach( $listw as $val ) {
									$oneUser = $Users->getItemByIdService( $val->users_u_id );
									
									$url = $this->serverName . $this->view->baseUrl() . '/shop/item/id/' . $id . '/' . $this->view->Seo( $post['name_pl'] );
									$oneUser = $oneUser->toArray();
									
									$nm = $oneUser['name'] . ' ' . $oneUser['surname'];
									$nm = ( $nm == ' ' ) ? $oneUser['email'] : $nm;
									
									$mail = new Core_Mail();
									$mail->addTo( $oneUser['email'], $nm );
									$mail->setFrom( 'no-reply@carnauba.pl', 'Carnauba' );
									$mail->setSubject( $this->view->translate( 'wishlist_topic' ) );
									$mail->setBodyHtml( 
										sprintf( 
											$this->view->translate( 'wishlist_body' ),
											$nm,
											$val['add_date'],
											$post['name_pl'],
											$url,
											$url
										)
									);
									
									$Wishlist->deleteItem( $val['w_id'] );
									
									$mail->send();
								}
							}

						}
						
						$CategriesHasProducts = new CategriesHasProducts();
						$CategriesHasProducts->deleteByProduct( $id );
						
						if( isSet( $post['categories'] ) && is_array( $post['categories'] ) ) {
							foreach( $post['categories'] as $value ) {
								$CategriesHasProducts = new CategriesHasProducts();
								$CategriesHasProducts->categries_c_id = $value;
								$CategriesHasProducts->products_p_id = $id;
								$CategriesHasProducts->save();
							}
						}
						
						$TagsHasProducts = new TagsHasProducts();
						$TagsHasProducts->deleteByProduct( $id );
						
						if( isSet( $post['tags'] ) && is_array( $post['tags'] ) ) {;
							foreach( $post['tags'] as $value ) {
								$TagsHasProducts = new TagsHasProducts();
								$TagsHasProducts->tags_t_id = $value;
								$TagsHasProducts->products_p_id = $id;
								$TagsHasProducts->save();
							}
						}
						
						$conn->commit();
	
						$this->_redirect( 'cpproducts/item2/id/' . $id );
					
					} catch( Exception $e ) {
						Core_Logs::log( 'carnauba', 'cpproducts.exception', 'msg: ' . $e->getMessage() );
						Core_Logs::log( 'carnauba', 'cpproducts.exception', 'code: ' . $e->getCode() );
						Core_Logs::log( 'carnauba', 'cpproducts.exception', 'file: ' . $e->getFile() );
						Core_Logs::log( 'carnauba', 'cpproducts.exception', 'line: ' . $e->getLine() );
						Core_Logs::log( 'carnauba', 'cpproducts.exception', 'trace: ' . $e->getTraceAsString() );
						$conn->rollback();
						$this->_redirect( 'cpproducts/list/oper/3' );
					}
				} else {
					foreach( $post as $key => $value ) {

						if( 
							( $key != 'categories' ) && 
							( $key != 'tags' ) && 
							( strlen( $value ) > 0 ) 
						) {
							$this->_products->{$key} = $value;
						}

						$this->view->{$key} = $value;
					}
					
					$this->_products->add_date = Date( 'Y-m-d H:i:s' );
					$this->_products->save();

					if( $this->_products->p_id > 0 ) {
						if( isSet( $post['categories'] ) && is_array( $post['categories'] ) ) {
							foreach( $post['categories'] as $value ) {
								$CategriesHasProducts = new CategriesHasProducts();
								$CategriesHasProducts->categries_c_id = $value;
								$CategriesHasProducts->products_p_id = $this->_products->p_id;
								$CategriesHasProducts->save();
							}
						}
						
						if( isSet( $post['tags'] ) && is_array( $post['tags'] ) ) {
							foreach( $post['tags'] as $value ) {
								$TagsHasProducts = new TagsHasProducts();
								$TagsHasProducts->tags_t_id = $value;
								$TagsHasProducts->products_p_id = $this->_products->p_id;
								$TagsHasProducts->save();
							}
						}

						$this->_redirect( 'cpproducts/item2/id/' . $this->_products->p_id );
					}
				}
			}

		} else {
			if( $id ) {
				$this->view->p_id = $id;

				$item = $this->_products->getItem( $id )->getFirst()->toArray();
				
				$CategriesHasProducts = new CategriesHasProducts();
				foreach( $CategriesHasProducts->getByProduct( $id )->toArray() as $value ) {
					$this->view->categories[] = $value['categries_c_id'];
				}
				
				$TagsHasProducts = new TagsHasProducts();
				foreach( $TagsHasProducts->getByProduct( $id )->toArray() as $value ) {
					$this->view->tags[] = $value['tags_t_id'];
				}

				foreach( $item as $key => $value ) {
					$this->view->{$key} = $value;
				}
			}
		
		}
		
		$this->assignType( 0 );
	}
	
	public function item2Action() {
		$id = intval( $this->_request->getParam( 'id', 0 ) );
		
		$this->view->magazine = intval( $this->_request->getParam( 'magazine', 0 ) );

		$this->view->p_id = $id;

		$item = $this->_products->getItem( $id )->getFirst()->toArray();
		if( $item ) {
			$Dimension = new Dimension();
			$this->view->dimensionList = $Dimension->getListByProduct( $id );
		} else {
			$this->_redirect( 'cpproducts/list/oper/3' );
		}
	}
	
	public function itemspecialAction() {
		$Vat = new Vat();
		$this->view->vatList = $Vat->getList();
		
		$Categories = new Categries();
		$this->view->categoriesList = $Categories->getList( 1 );
		
		$ProductsSpecial = new ProductsSpecial();

		$this->view->categories = array(0);
		
		$id = intval( $this->_request->getParam( 'id', 0 ) );
		
		if( $id ) {
			$this->view->ps_id = $id;

			$item = $ProductsSpecial->getItem( $id )->getFirst()->toArray();
		
			foreach( $item as $key => $value ) {
				$this->view->{$key} = $value;
			}

			$CategriesHasProductsSpecial = new CategriesHasProductsSpecial();
			foreach( $CategriesHasProductsSpecial->getByProduct( $id )->toArray() as $value ) {
				$this->view->categories[] = $value['categries_c_id'];
			}
		}
		
		if( $this->_request->isPost() ) {

			$post = $this->_request->getPost();

			if( $id ) {
				$this->view->p_id = $id;

				$conn = Doctrine_Manager::getInstance()->getCurrentConnection();
				
				try {
					$conn->beginTransaction();
				
					$item = $ProductsSpecial->getItem( $id )->getFirst();
					foreach( $post as $key => $value ) {
	
						if( 
							( $key != 'categories' ) 
						) {
							$item->{$key} = $value;
						}

						$this->view->{$key} = $value;
					}
					$item->save();
					
					if( isSet( $post['categories'] ) && is_array( $post['categories'] ) ) {
						$CategriesHasProductsSpecial = new CategriesHasProductsSpecial();
						$CategriesHasProductsSpecial->deleteByProduct( $id );

						foreach( $post['categories'] as $value ) {
							$CategriesHasProductsSpecial = new CategriesHasProductsSpecial();
							$CategriesHasProductsSpecial->categries_c_id = $value;
							$CategriesHasProductsSpecial->products_special_ps_id = $id;
							$CategriesHasProductsSpecial->save();
						}
					}

					$conn->commit();

					$this->_redirect( 'cpproducts/listspecial/oper/2' );
				
				} catch( Exception $e ) {
					Core_Logs::log( 'carnauba', 'cpproducts.exception', 'msg: ' . $e->getMessage() );
					Core_Logs::log( 'carnauba', 'cpproducts.exception', 'code: ' . $e->getCode() );
					Core_Logs::log( 'carnauba', 'cpproducts.exception', 'file: ' . $e->getFile() );
					Core_Logs::log( 'carnauba', 'cpproducts.exception', 'line: ' . $e->getLine() );
					Core_Logs::log( 'carnauba', 'cpproducts.exception', 'trace: ' . $e->getTraceAsString() );
					$conn->rollback();
					$this->_redirect( 'cpproducts/listspecial/oper/3' );
				}
			} else {

				foreach( $post as $key => $value ) {

					if( 
						( $key != 'categories' ) && 
						( strlen( $value ) > 0 ) 
					) {
						$ProductsSpecial->{$key} = $value;
					}

					$this->view->{$key} = $value;
				}

				$ProductsSpecial->save();

				if( $ProductsSpecial->ps_id > 0 ) {
					if( isSet( $post['categories'] ) && is_array( $post['categories'] ) ) {
						foreach( $post['categories'] as $value ) {
							$CategriesHasProductsSpecial = new CategriesHasProductsSpecial();
							$CategriesHasProductsSpecial->categries_c_id = $value;
							$CategriesHasProductsSpecial->products_special_ps_id = $ProductsSpecial->ps_id;
							$CategriesHasProductsSpecial->save();
						}
					}
					$this->_redirect( 'cpproducts/listspecial/oper/1' );
				} else {
					$this->_redirect( 'cpproducts/listspecial/oper/3' );
				}

			}

		}
		
		$this->assignType( 1 );
	}
	
	public function linkproductsAction() {
		$this->_helper->layout->disableLayout();
		$this->_helper->viewRenderer->setNoRender();
		
		$id = $this->_request->getParam( 'id' );
		$p_id = $this->_request->getParam( 'p_id' );
		$oper = $this->_request->getParam( 'oper', 'add' );
		$special = $this->_request->getParam( 'special', 1 );
		
		if( $special ) {
			$LinkProductsSpecial = new LinkProductsSpecial();
			if( $oper == 'add' ) {
				$LinkProductsSpecial->products_special_ps_id = $id;
				$LinkProductsSpecial->owner_id = $p_id;
				$LinkProductsSpecial->save();
			} else if( $oper == 'del' ) {
				$LinkProductsSpecial->deleteItem( $id, $p_id );
			}			
		} else {
			$LinkProducts = new LinkProducts();
			if( $oper == 'add' ) {
				$LinkProducts->products_p_id = $id;
				$LinkProducts->owner_id = $p_id;
				$LinkProducts->save();
			} else if( $oper == 'del' ) {
				$LinkProducts->deleteItem( $id, $p_id );
			}
		}
		
		echo Zend_Json::encode(array(
			'status' => 'OK'
		));
	}
	
	public function getproductsAction() {
		$this->_helper->layout->disableLayout();
		//$this->_helper->viewRenderer->setNoRender();

		$id = $this->_request->getParam( 'id' );
		$filtr = $this->_request->getParam( 'filtr', 'WSZYSTKIE' );
		$page = $this->_request->getParam( 'page', 1 );
		$special = $this->_request->getParam( 'special', 0 );

		$this->view->filtrn = $filtr;

		if( $filtr == 'WSZYSTKIE' ) {
			$filtr = false;
		}
		
		if( $special ) {

			$pagerLayout = $this->_products->getListSpecialByFiltr( $page, $filtr, $this->view->baseUrl() );
			
			$this->view->list = $pagerLayout->execute();
			$this->view->pager = $pagerLayout;
	
			$LinkProductsSpecial = new LinkProductsSpecial();
			$linkProd = $LinkProductsSpecial->getListByProduct( $id )->toArray();
	
			$ltemp = array();
			foreach( $linkProd as $value ) {
				$ltemp[] = $value['products_special_ps_id'];
			}
			
			$this->view->special = true;
			
		} else {
			$pagerLayout = $this->_products->getListByFiltr( $page, $filtr, $this->view->baseUrl() );
			
			$this->view->list = $pagerLayout->execute();
			$this->view->pager = $pagerLayout;
	
			$LinkProducts = new LinkProducts();
			$linkProd = $LinkProducts->getListByProduct( $id )->toArray();
	
			$ltemp = array();
			foreach( $linkProd as $value ) {
				$ltemp[] = $value['products_p_id'];
			}
		}
		$this->view->linkProductsList = $ltemp;
	}
	
	public function savedimensionAction() {
		$this->_helper->layout->disableLayout();
		$this->_helper->viewRenderer->setNoRender();
		
		$post = $this->_request->getPost();
		
		$edit = false;
		if( intval( $post['d_id'] ) > 0 ) {
			
			$Dimension = new Dimension();
			$item = $Dimension->getItem( $post['d_id'] )->getFirst();
			
			if( $item['stock'] < 1 && $post > 0 ) {
				$BackInStock = new BackInStock();
				$list = $BackInStock->getToSend( $post['products_p_id'], $post['d_id'] );
				
				foreach( $list as $val ) {
					
					$prod = $val['Products']->toArray();

					$pUrl = $this->serverName . $this->view->baseUrl() . '/shop/item/id/' . $val['products_p_id'] . '/' . $this->view->Seo( $prod['name_pl'] );
					
					$mail = new Core_Mail();
					$mail->addTo( $val['email'], $val['email'] );
					$mail->setFrom( 'no-reply@carnauba.pl', 'Carnauba' );
					$mail->setSubject( $this->view->translate( 'back_in_stock_topic' ) );
					$mail->setBodyHtml( 
						sprintf( 
							$this->view->translate( 'back_in_stock_body' ),
							$val['email'],
							$prod['name_pl'],
							$pUrl,
							$pUrl
						)
					);
					
					$BackInStock->deleteItem( $val['bis_id'] );
					
					$mail->send();
				}
			}
			
			foreach( $post as $key => $value ) {
				if( strlen( $value ) > 0 ) {					
					if( in_array( $key, array( 'price1', 'price2', 'price3', 'price4', 'price5' ) ) ) {
						$item->{$key} = str_replace( ',', '.', $value );
					} else {
						$item->{$key} = $value;
					}
				}
			}
			
			$item->save();
			echo Zend_Json::encode(array(
				'status' => 'OK',
				'name' => $post['name'],
				'id' => $post['d_id'],
				'edit' => 1
			));
			
		} else {

			$Dimension = new Dimension();
			foreach( $post as $key => $value ) {
				if( strlen( $value ) > 0 ) {
					if( in_array( $key, array( 'price1', 'price2', 'price3', 'price4', 'price5' ) ) ) {
						$Dimension->{$key} = str_replace( ',', '.', $value );
					} else {
						$Dimension->{$key} = $value;
					}
					
				}
			}

			$Dimension->save();
			echo Zend_Json::encode(array(
				'status' => 'OK',
				'id' => $Dimension->d_id,
				'edit' => 0
			));
		}
	}
	
	public function generatespecialAction() {
		$this->_helper->layout->disableLayout();
		$this->_helper->viewRenderer->setNoRender();
		 
		$post = $this->_request->getPost();

		$images = new Core_Images( getcwd() . $this->config['dirs']['upload'] . $post['img'] );
		$images->thumb( 100, 100 );
		$images = new Core_Images( getcwd() . $this->config['dirs']['upload'] . $post['img'] );
		$images->thumb( 170, 170 );
		$images = new Core_Images( getcwd() . $this->config['dirs']['upload'] . $post['img'] );
		$images->thumb( 195, 167 );
		$images = new Core_Images( getcwd() . $this->config['dirs']['upload'] . $post['img'] );
		$images->thumb( 220, 220 );
		$images = new Core_Images( getcwd() . $this->config['dirs']['upload'] . $post['img'] );
		$images->thumb( 293, 220 );
		
		echo Zend_Json::encode(array(
			'status' => 'OK',
			'img' => $this->view->File( $post['img'], 170 )
		));
	}
	
	public function deletedimensionAction() {
		$this->_helper->layout->disableLayout();
		$this->_helper->viewRenderer->setNoRender();

		$id = $this->_request->getParam( 'id' );

		$Dimension = new Dimension();
		$Dimension->deleteItem( $id );

		echo Zend_Json::encode(array(
			'status' => 'OK'
		));
	}
	
	public function editdimensionAction() {
		$this->_helper->layout->disableLayout();
		$this->_helper->viewRenderer->setNoRender();

		$id = $this->_request->getParam( 'id' );

		$Dimension = new Dimension();
		
		$data = $Dimension->getItem( $id )->getFirst()->toArray();
		
		$data['price1'] = number_format( $data['price1'], 2, '.', '' );
		$data['price2'] = number_format( $data['price2'], 2, '.', '' );
		$data['price3'] = number_format( $data['price3'], 2, '.', '' );
		$data['price4'] = number_format( $data['price4'], 2, '.', '' );
		$data['price5'] = number_format( $data['price5'], 2, '.', '' );

		echo Zend_Json::encode(array(
			'status' => 'OK',
			'data' => $data
		));
	}
	
	public function listAction() {
		$page = intval( $this->_request->getParam( 'page', 1 ) );
		
		$this->view->producent = $this->_request->getParam('producent', '');
		$this->view->kategoria = $this->_request->getParam('kategoria', '');
		$this->view->podkategoria = $this->_request->getParam('podkategoria', '');
		$this->view->tag = $this->_request->getParam('tag', '');
		$this->view->nazwa = $this->_request->getParam('nazwa', '');
		
		$tagi = new Tags();
		$tag = $tagi->getList();
		$list=array(0=>'');
		foreach($tag as $jeden)
			$list[$jeden['t_id']]=$jeden['name_pl'];  
		$this->view->tag_list=$list;
		$tab_in = array();
		
		if($this->view->tag!='0' AND $this->view->tag!='')
		{
			$tagi = new TagsHasProducts();
			$tag_in = $tagi->getListProd($this->view->tag);
			$tag_tab = array();
			foreach($tag_in as $tag)
				$tab_in[]=$tag['products_p_id'];
		}
		
		
		$tagi = new Categries();
		$tag = $tagi->getListAll();
		$list=array(0=>'');
		$list2=array(0=>'');
		foreach($tag as $jeden)
		{
			if($jeden['parent_id']==0)
				$list[$jeden['c_id']]=$jeden['name_pl'];
			else
				$list2[$jeden['c_id']]='	'.$jeden['name_pl'];
		}   
		$this->view->kat_list=$list;
		$this->view->podkat_list=$list2;
		
		if($this->view->kategoria!='0'  AND $this->view->kategoria!='')
		{
			$tagi = new CategriesHasProducts();
			$tag_in = $tagi->getListProd($this->view->kategoria);
			$kat_tab = array();
			foreach($tag_in as $tag)
				$tab_in[]=$tag['products_p_id'];
		}
		if($this->view->podkategoria!='0' AND $this->view->podkategoria!='')
		{
			$tagi = new CategriesHasProducts();
			$tag_in = $tagi->getListProd($this->view->podkategoria);
			$podkat_tab = array();
			foreach($tag_in as $tag)
				$tab_in[]=$tag['products_p_id'];
		}
		if(empty($tab_in))
			$tab_in = '';
		else
		{	
			$pomoc = '';
			$tab_in=array_unique($tab_in);
			foreach($tab_in as $one)
				$pomoc .= $one.',';
			$tab_in = substr($pomoc, 0 , -1); 
		}
		
		
		$man = new Manufacturers();
		$man_l = $man->getList();
		$list=array(0=>'');
		foreach($man_l as $jeden)
			$list[$jeden['m_id']]=$jeden['name_pl'];  
		$this->view->producent_list=$list;
		
		
		
		$pagerLayout = $this->_products->getListWithPager( $page, $this->view->baseUrl() , $this->view->producent, $this->view->nazwa, $tab_in,$this->view->kategoria,$this->view->podkategoria,$this->view->tag);
		
		$this->view->list = $pagerLayout->execute();
		$this->view->pager = $pagerLayout;

		$this->assignType( 0 );
	}
	
	public function listspecialAction() {
		$page = intval( $this->_request->getParam( 'page', 1 ) );

		$ProductsSpecial = new ProductsSpecial();
		$pagerLayout = $ProductsSpecial->getListWithPager( $page, $this->view->baseUrl() );

		$this->view->list = $pagerLayout->execute();
		$this->view->pager = $pagerLayout;

		$this->assignType( 1 );
	}
	
	public function deletespecialAction() {
		$this->_helper->layout->disableLayout();
		$this->_helper->viewRenderer->setNoRender();

		$id = intval( $this->_request->getParam( 'id', 0 ) );
		
		if( $id > 0 ) {
			$ProductsSpecial = new ProductsSpecial();
			$ProductsSpecial->deleteItem( $id );
			
			$this->_redirect( 'cpproducts/listspecial/oper/4' );
		} else {
			$this->_redirect( 'cpproducts/listspecial/oper/3' );
		}
	}
	
	public function deleteAction() {
		$this->_helper->layout->disableLayout();
		$this->_helper->viewRenderer->setNoRender();

		$id = intval( $this->_request->getParam( 'id', 0 ) );
		
		if( $id > 0 ) {
			$this->_products->deleteItem( $id );
			
			$this->_redirect( 'cpproducts/list/oper/4' );
		} else {
			$this->_redirect( 'cpproducts/list/oper/3' );
		}
	}
	
	public function wishlistAction() {
		if( $this->_request->isPost() ) {
			$post = $this->_request->getPost();

			$Users = new Users();
			
			$post['from'] = empty( $post['from'] ) ? false : $post['from'];
			$post['to'] = empty( $post['to'] ) ? false : $post['to'];
			
			$this->view->from = $post['from'];
			$this->view->to = $post['to'];
			
			$this->view->list = $this->_products->getWishList( $post['from'], $post['to'] );

		} else {
			$this->view->list = $this->_products->getWishList( 0, 0 );
		}

		if( isSet( $post['excel'] ) && $post['excel'] ) {
			$this->_helper->layout->disableLayout();
			$this->_helper->viewRenderer->setNoRender();
			
			$arr = array();
			foreach( $this->view->list as $item ){

				if( !isSet( $arr[$item['p_id']] ) ) {
					$arr[$item['p_id']]['item'] = $item;
					$arr[$item['p_id']]['cnt'] = 1;
				} else {
					$arr[$item['p_id']]['cnt'] = $arr[$item['p_id']]['cnt'] + 1;					
				}
				
			}

			echo 'Id_produktu;Nazwa;Ilość' . "\n";

			foreach( $arr as $item ) {
				echo $item['item']['p_id'] . ';' . $item['item']['name_pl']  . ';' . $item['cnt'] . "\n";
			}

			$this->getResponse()->setHeader( 'Content-type', 'application/txt' );
			$this->getResponse()->setHeader( 'Content-Disposition', 'attachment; filename="wishlist.csv"' );
		}

		$this->assignType( 2 );
	}
	
	public function assignType( $type ) {
		
		$this->view->type = $type;
		
		switch( $type ) {
			case 0 :
				$this->view->yih2 = 'Produkty sklepu';
				break;
			case 1 :
				$this->view->yih2 = 'Produkty specjalne';
				break;
			case 2 :
				$this->view->yih2 = 'Lista wishlist';
				break;
		}
	}
}